#include <ios>
#include <iostream>
#include <unordered_map>
#include <string>
#include <fstream>
#include <sstream>

using std::cout;
using std::endl;
using std::unordered_map;
using std::string;
using std::ifstream;
using std::istringstream;

class TransWord
{
public:
    void buildMap(const string &filename)
    {
        /* ifstream ifs(filename, std::ios::ate); */
        ifstream ifs(filename);
        if(!ifs)
        {
            std::cerr << "ifstream is not good" <<endl;
            return;
        }

        /* ifs.seekg(79); */
        /* ifs.seekg(79, std::ios::beg); */
        /* ifs.seekg(0, std::ios::end); */
        /* ifs.seekg(-79, std::ios::end); */

        /* cout << "ifs.tellg() = " << ifs.tellg() << endl; */

        string key;
        string line;
        while(ifs >> key, getline(ifs, line))//
        {
            if(line.size() > 1)
            {
                _um[key] = line.substr(1);//取子串
            }
        }

        ifs.close();
    }

    void trans(const string &filename)
    {
        ifstream ifs(filename);
        if(!ifs)
        {
            std::cerr << "ifstream is not good" <<endl;
            return;
        }

        string line;
        while(getline(ifs, line))
        {
            istringstream iss(line);
            string word;
            while(iss >> word)
            {
                auto it = _um.find(word);
                if(it != _um.end())
                {
                    cout << it->second;
                }
                else
                {
                    cout << word;
                }
                cout << " ";
#if 0
                size_t cnt = _um.count(word);
                if(1 == cnt)
                {
                    cout << _um[word];
                }
                else
                {
                    cout << word;
                }
#endif
            }
            cout << endl;
        }

        ifs.close();

    }
private:
    unordered_map<string, string> _um;//存放key与value
};

void test()
{

}

int main(int argc, char *argv[])
{
    TransWord tw;
    tw.buildMap("map.txt");
    tw.trans("file.txt");
    return 0;
}

